package com.ttdevs.ttrss.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DBHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "ttdevsrss.db";

	private static final int DATABASE_VERSION = 4;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		TableGroup.creatTable(db);
		TableSource.creatTable(db);
		TableArticle.creatTable(db);
		TableImage.creatTable(db);
		TableCnbeta.creatTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		TableGroup.dropTable(db);
		TableSource.dropTable(db);
		TableArticle.dropTable(db);
		TableImage.dropTable(db);
		TableCnbeta.dropTable(db);
		
		onCreate(db);
	}

	public interface IBaseTable extends BaseColumns {
		public static final String TYPE_TEXT = " text ";
		public static final String TYPE_INTEGER = " integer ";
		public static final String SEP = " , ";
	}

	public static class TableGroup implements IBaseTable {
		public final static String TABLE_NAME = "groupinfo";
		public final static String NAME = "name";
		public final static String ORDER = "orderkey";

		private static void creatTable(SQLiteDatabase db) {
			String sqlCreateTable = "CREATE TABLE " + TABLE_NAME + "(" + _ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT " + SEP + NAME
					+ TYPE_TEXT + SEP + ORDER + TYPE_TEXT + ")";
			
			db.execSQL(sqlCreateTable);
			
			ContentValues cv = new ContentValues();
			cv.put(NAME, "默认分组");
			cv.put(ORDER, "!");
			db.insert(TABLE_NAME, null, cv);
		}

		private static void dropTable(SQLiteDatabase db) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		}
	}

	public static class TableSource implements IBaseTable {
		public final static String TABLE_NAME = "sourceinfo";

		public final static String TITLE = "title";
		public final static String LINK = "link";
		public final static String DESCRIPTION = "description";
		public final static String ICON = "icon";
		public final static String SRCIDQQ = "sourceid";
		public final static String GROUP = "belonggroup";
		public final static String LASTBUILDDATE = "lastbuilddate";
		public final static String WIFIAUTOUPDATE = "wifiautoupdate";
		public final static String FREQUENCY = "frequency";
		public final static String ORDER = "orderkey";
		public final static String CREATETIME = "createtime";

		private static void creatTable(SQLiteDatabase db) {
			String sqlCreateTable = "CREATE TABLE " + TABLE_NAME + "(" + _ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT " + SEP + TITLE
					+ TYPE_TEXT + SEP + DESCRIPTION + TYPE_TEXT + SEP + LINK + TYPE_TEXT + SEP + ICON + TYPE_TEXT + SEP + SRCIDQQ
					+ TYPE_TEXT + SEP + GROUP + TYPE_TEXT + SEP + LASTBUILDDATE + TYPE_INTEGER + SEP + WIFIAUTOUPDATE + TYPE_INTEGER + SEP
					+ FREQUENCY + TYPE_INTEGER + SEP + ORDER + TYPE_TEXT + SEP + CREATETIME + TYPE_INTEGER + ")";
			db.execSQL(sqlCreateTable);

			String sqlCreateIndex = "create unique index " + LINK + " on " + TABLE_NAME + "(" + LINK + ")";
			db.execSQL(sqlCreateIndex);
		}

		private static void dropTable(SQLiteDatabase db) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		}
	}

	public static class TableArticle implements IBaseTable {
		public final static String TABLE_NAME = "articleinfo";

		public final static String TITLE = "title";
		public final static String LINK = "link";
		public final static String ICON = "icon";
		public final static String DESC = "description";
		public final static String AUTHOR = "author";
		public final static String PUBDATE = "pubDate";
		public final static String GUID = "guid";
		public final static String SOURCE = "source";
		public final static String MARK = "mark";
		public final static String READ = "read";
		public final static String CREATETIME = "createtime";

		private static void creatTable(SQLiteDatabase db) {
			String sqlCreateTable = "CREATE TABLE " + TABLE_NAME + "(" + _ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT " + SEP + TITLE
					+ TYPE_TEXT + SEP + LINK + TYPE_TEXT + SEP + ICON + TYPE_TEXT + SEP + DESC + TYPE_TEXT + SEP + AUTHOR + TYPE_TEXT + SEP
					+ PUBDATE + TYPE_INTEGER + SEP + GUID + TYPE_TEXT + SEP + SOURCE + TYPE_TEXT + SEP + MARK + TYPE_INTEGER + SEP + READ
					+ TYPE_INTEGER + SEP + CREATETIME + TYPE_INTEGER + ")";

			db.execSQL(sqlCreateTable);
		}

		private static void dropTable(SQLiteDatabase db) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		}
	}

	public static class TableImage implements IBaseTable {
		public final static String TABLE_NAME = "imageinfo";
		public final static String URL = "url";
		public final static String THUMBPATH = "thumbpath";
		public final static String LOACLPATH = "localpath";

		private static void creatTable(SQLiteDatabase db) {
			String sqlCreateTable = "CREATE TABLE " + TABLE_NAME + "(" + _ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT "
					+ SEP + URL + TYPE_TEXT + SEP + THUMBPATH + TYPE_TEXT + SEP + LOACLPATH + TYPE_TEXT + ")";

			db.execSQL(sqlCreateTable);
		}

		private static void dropTable(SQLiteDatabase db) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		}
	}
	
	public static class TableCnbeta implements IBaseTable {
		public final static String TABLE_NAME = "cnbeta";
		public final static String TITLE = "title";
		public final static String PUBTIME = "pubtime";
		public final static String ARTICLEID = "articleid";
		public final static String CMTCLOSED = "cmtclosed";
		public final static String CMTNUM = "cmtnum";
		public final static String SUMMARY = "summary";
		public final static String TOPICLOGO = "topiclogo";
		public final static String THEME = "theme";
		public final static String READ = "read";
		public final static String MARK = "mark";
		public final static String CREATETIME = "createtime";

		private static void creatTable(SQLiteDatabase db) {
			String sqlCreateTable = "CREATE TABLE " + TABLE_NAME + "(" + 
					_ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT " + SEP + 
					TITLE + TYPE_TEXT + SEP + 
					PUBTIME + TYPE_TEXT + SEP + 
					ARTICLEID + TYPE_INTEGER + SEP + 
					CMTCLOSED + TYPE_INTEGER + SEP + 
					CMTNUM + TYPE_INTEGER + SEP + 
					SUMMARY + TYPE_TEXT + SEP + 
					TOPICLOGO + TYPE_TEXT + SEP + 
					THEME + TYPE_TEXT + SEP + 
					READ + TYPE_INTEGER + SEP + 
					MARK + TYPE_INTEGER + SEP +
					CREATETIME + TYPE_INTEGER +
					")";

			db.execSQL(sqlCreateTable);
		}

		private static void dropTable(SQLiteDatabase db) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		}
	}
}
